﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Web;
using System.Data;

    public class pagingUtil
    {
        public static string pagingSql(string sSql, int currentPage, string jsAjax,int page_size)
        {
            string sParam = "";
            //paging
            int pg = 1;
            pg = currentPage;
            int start = page_size * pg;
            //DataTable dtBidding = DBUtil.LoadPage(sSql, start, page_size);

            //end paging

            string sSqlTotal = "select count(id) from (" + sSql + ") as recTemp";
            DataTable dt = DBUtil.SelectTable(sSqlTotal);
            int totalRecord = int.Parse(dt.Rows[0][0].ToString());

            string sPaging = "<div style='margin:0 auto;text-align:right;'>";
            sPaging += "<div id='pagination'>";
            if (totalRecord > page_size)
            {
                int totalIndex = totalRecord / page_size + (totalRecord % page_size == 0 ? 0 : 1);


                //lấy số group có thể chia thành
                int[] numGroup = getGroupNumber(totalIndex, pageSizeGroup);

                //lấy group của trang đang load hiện tại là group nào?
                int currentGroup = getGroupBelongTo(currentPage, pageSizeGroup);

                //nếu ko là đầu
                if (currentGroup != 0 && numGroup.Length > 1) { sPaging += "<a href='javascript:" + jsAjax + "(" + (0 + 1).ToString() + sParam + ");'>Trước</a> . "; }


                //in ra số trang trong group hiện tại
                for (int jp = 0; jp <= pageSizeGroup - 1; jp++)
                {
                    //if (((currentGroup) * pageSizeGroup  +1) > totalIndex) break;

                    if ((currentGroup * pageSizeGroup + (jp + 1)) * page_size > totalRecord) break;
                    string indexPage = (currentGroup * pageSizeGroup + (jp + 1)).ToString();
                    if ((currentGroup * pageSizeGroup + (jp + 1)) != currentPage)
                    {

                        sPaging += "<a href='javascript:" + jsAjax + "(" + (currentGroup * pageSizeGroup + jp + 1).ToString() + sParam + ");'>" + indexPage + "</a>";
                    }
                    else
                    {
                        sPaging += "<span class='maudophantrang'>" + indexPage + "</span>";
                    }
                    if (jp != pageSizeGroup - 1) sPaging = sPaging + " . ";
                }

                //nếu ko là cuối
                if (currentGroup != numGroup[numGroup.Length - 1] && numGroup.Length > 1) { sPaging += ". <a href='javascript:" + jsAjax + "(" + ((currentGroup + 1) * pageSizeGroup).ToString() + sParam + ");'>Sau</a>"; }

                ////for (int jp = 0; jp < totalIndex; jp++)
                ////{
                ////    string indexPage = (jp + 1).ToString();
                ////    if ((jp + 1) != currentPage)
                ////    {

                ////        sPaging += "<a href='javascript:" + jsAjax + "(" + (jp + 1).ToString() + ");'>" + indexPage + "</a> . ";
                ////    }
                ////    else
                ////    {
                ////        sPaging += "<span class='maudophantrang'>" + indexPage + "</span> . ";
                ////    }
                ////}
            }
            sPaging += "</div>";
            sPaging += "</div>";
            return sPaging;
        }

        public static string pagingSql(string sSql,int currentPage,string jsAjax)
        {
            string sParam = "";
            //paging
            int pg = 1;
            if (HttpContext.Current.Request.QueryString["page"] != null) pg = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]) - 1;
            int page_size = comm.GetVariantPageSize();
            int start = page_size * pg;
            DataTable dtBidding = DBUtil.LoadPage(sSql, start, page_size);
           
            //end paging

            string sSqlTotal = "select count(id) from (" + sSql + ") as recTemp";
            DataTable dt = DBUtil.SelectTable(sSqlTotal);
            int totalRecord = int.Parse(dt.Rows[0][0].ToString());

            string sPaging = "<div style='margin:0 auto;text-align:right;'>";
            sPaging+="<div id='pagination'>";
            if (totalRecord > page_size)
            {
                int totalIndex = totalRecord / page_size + (totalRecord % page_size == 0 ? 0 : 1);

                //lấy số group có thể chia
                int[] numGroup = getGroupNumber(totalIndex, pageSizeGroup);

                //lấy group của trang đang load hiện tại
                int currentGroup = getGroupBelongTo(currentPage, pageSizeGroup);

                //nếu ko là đầu
                if (currentGroup != 0 && numGroup.Length > 1) { sPaging += "<a href='javascript:" + jsAjax + "(" + (0 + 1).ToString() + sParam + ");'>Trước</a> . "; }


                //in ra số trang trong group hiện tại
                for (int jp = 0; jp <= pageSizeGroup - 1; jp++)
                {
                    if (currentGroup * pageSizeGroup + (jp + 1) > totalIndex) break;
                    if ((currentGroup * pageSizeGroup + (jp + 1)) * page_size > totalRecord) break;

                    string indexPage = (currentGroup * pageSizeGroup + (jp + 1)).ToString();
                    if ((currentGroup * pageSizeGroup + (jp + 1)) != currentPage)
                    {

                        sPaging += "<a href='javascript:" + jsAjax + "(" + (currentGroup * pageSizeGroup + jp + 1).ToString() + sParam + ");'>" + indexPage + "</a>";
                    }
                    else
                    {
                        sPaging += "<span class='maudophantrang'>" + indexPage + "</span>";
                    }
                    if (jp != pageSizeGroup - 1) sPaging = sPaging + " . ";
                }

                //nếu ko là cuối
                if (currentGroup != numGroup[numGroup.Length - 1] && numGroup.Length > 1) { sPaging += ". <a href='javascript:" + jsAjax + "(" + ((currentGroup + 1) * pageSizeGroup).ToString() + sParam + ");'>Sau</a>"; }


                ////for (int jp = 0; jp < totalIndex; jp++)
                ////{
                ////    string indexPage = (jp + 1).ToString();
                ////    if ((jp+1) != currentPage)
                ////    {

                ////        sPaging += "<a href='javascript:"+jsAjax+"(" + (jp + 1).ToString() + ");'>" + indexPage + "</a>   ";
                ////    }
                ////    else
                ////    {
                ////        sPaging += "<span class='maudophantrang'>" + indexPage + "</span>   ";       
                ////   }
                ////}   
            }      
            sPaging+="</div>";
            sPaging+="</div>";
            return sPaging;
        }

        public static string pagingSqlOrderable(string sSql,int page_size, int currentPage, string jsAjax)
        {
            string sParam = "";

            //paging
            int pg = 1;
            if (HttpContext.Current.Request.QueryString["page"] != null) pg = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]) - 1;
            //int page_size = comm.GetVariantPageSize();
            int start = page_size * pg;
            DataTable dtBidding = DBUtil.LoadPage(sSql, start, page_size);

            //end paging

            //string sSqlTotal = "select count(id) from (" + sSql + ") as recTemp";
            DataTable dt = DBUtil.SelectTable(sSql);//sSqlTotal
            int totalRecord = dt.Rows.Count;

            string sPaging = "<div style='margin:0 auto;text-align:right;'>";
            sPaging += "<div id='pagination'>";
            if (totalRecord > page_size)
            {
                int totalIndex = totalRecord / page_size + (totalRecord % page_size == 0 ? 0 : 1);

                //lấy số group có thể chia
                int[] numGroup = getGroupNumber(totalIndex, pageSizeGroup);

                //lấy group của trang đang load hiện tại
                int currentGroup = getGroupBelongTo(currentPage, pageSizeGroup);

                //nếu ko là đầu
                if (currentGroup != 0 && numGroup.Length > 1) { sPaging += "<a href='javascript:" + jsAjax + "(" + (0 + 1).ToString() + sParam + ");'>Trước</a> . "; }


                //in ra số trang trong group hiện tại
                for (int jp = 0; jp <= pageSizeGroup - 1; jp++)
                {
                    if (currentGroup * pageSizeGroup + (jp + 1) > totalIndex) break;
                    if ((currentGroup * pageSizeGroup + (jp + 1)) * page_size > totalRecord) break;

                    string indexPage = (currentGroup * pageSizeGroup + (jp + 1)).ToString();
                    if ((currentGroup * pageSizeGroup + (jp + 1)) != currentPage)
                    {

                        sPaging += "<a href='javascript:" + jsAjax + "(" + (currentGroup * pageSizeGroup + jp + 1).ToString() + sParam + ");'>" + indexPage + "</a>";
                    }
                    else
                    {
                        sPaging += "<span class='maudophantrang'>" + indexPage + "</span>";
                    }
                    if (jp != pageSizeGroup - 1) sPaging = sPaging + " . ";
                }

                //nếu ko là cuối
                if (currentGroup != numGroup[numGroup.Length - 1] && numGroup.Length > 1) { sPaging += ". <a href='javascript:" + jsAjax + "(" + ((currentGroup + 1) * pageSizeGroup).ToString() + sParam + ");'>Sau</a>"; }

                ////for (int jp = 0; jp < totalIndex; jp++)
                ////{
                ////    string indexPage = (jp + 1).ToString();
                ////    if ((jp + 1) != currentPage)
                ////    {

                ////        sPaging += "<a href='javascript:" + jsAjax + "(" + (jp + 1).ToString() + ");'>" + indexPage + "</a>   ";
                ////    }
                ////    else
                ////    {
                ////        sPaging += "<span class='maudophantrang'>" + indexPage + "</span>   ";
                ////    }
                ////}
            }
            sPaging += "</div>";
            sPaging += "</div>";
            return sPaging;
        }

        public static string pagingSqlMutilParam(string sSql, int currentPage, string jsAjax,string[] arrParam)
        {
            string sParam = "";

            foreach (string s in arrParam) 
            {
                //if (sParam == "") sParam = s;
                //else 
                    sParam += "," + s;
            }

            //paging
            int pg = 1;
            if (HttpContext.Current.Request.QueryString["page"] != null) pg = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]) - 1;
            int page_size = comm.GetVariantPageSize();
            int start = page_size * pg;
            DataTable dtBidding = DBUtil.LoadPage(sSql, start, page_size);

            //end paging

            string sSqlTotal = "select count(id) from (" + sSql + ") as recTemp";
            DataTable dt = DBUtil.SelectTable(sSqlTotal);
            int totalRecord = int.Parse(dt.Rows[0][0].ToString());

            string sPaging = "<div style='margin:0 auto;text-align:center;'>";
            sPaging += "<div id='pagination'>";
            if (totalRecord > page_size)
            {
                int totalIndex = totalRecord / page_size+ (totalRecord % page_size == 0 ? 0 : 1);


                //lấy số group có thể chia
                int[] numGroup = getGroupNumber(totalIndex, pageSizeGroup);

                //lấy group của trang đang load hiện tại
                int currentGroup = getGroupBelongTo(currentPage, pageSizeGroup);

                //nếu ko là đầu
                if (currentGroup != 0 && numGroup.Length > 1) { sPaging += "<a href='javascript:" + jsAjax + "(" + (0 + 1).ToString() + sParam + ");'>Trước</a> . "; }


                //in ra số trang trong group hiện tại
                for (int jp = 0; jp <= pageSizeGroup - 1; jp++)
                {
                    if (currentGroup * pageSizeGroup + (jp + 1) > totalIndex) break;
                    if ((currentGroup * pageSizeGroup + (jp + 1)) * page_size > totalRecord) break;

                    string indexPage = (currentGroup * pageSizeGroup + (jp + 1)).ToString();
                    if ((currentGroup * pageSizeGroup + (jp + 1)) != currentPage)
                    {

                        sPaging += "<a href='javascript:" + jsAjax + "(" + (currentGroup * pageSizeGroup + jp + 1).ToString() + sParam + ");'>" + indexPage + "</a>";
                    }
                    else
                    {
                        sPaging += "<span class='maudophantrang'>" + indexPage + "</span>";
                    }
                    if (jp != pageSizeGroup - 1) sPaging = sPaging + " . ";
                }

                //nếu ko là cuối
                if (currentGroup != numGroup[numGroup.Length - 1] && numGroup.Length > 1) { sPaging += ". <a href='javascript:" + jsAjax + "(" + ((currentGroup + 1) * pageSizeGroup).ToString() + sParam + ");'>Sau</a>"; }

                ////for (int jp = 0; jp < totalIndex; jp++)
                ////{
                ////    string indexPage = (jp + 1).ToString();
                ////    if ((jp+1) != currentPage)
                ////    {

                ////        sPaging += "<a href='javascript:" + jsAjax + "(" + (jp + 1).ToString() + sParam + ");'>" + indexPage + "</a>   ";
                ////    }
                ////    else
                ////    {
                ////        sPaging += "<span class='maudophantrang'>" + indexPage + "</span>   ";
                ////    }
                ////}
            }
            sPaging += "</div>";
            sPaging += "</div>";
            return sPaging;
        }
        public static int[] getGroupNumber(int totalRecord, int groupPage)
        {
            int numberGroup = totalRecord / groupPage + (totalRecord % groupPage == 0 ? 0 : 1);
            int[] arr = new int[numberGroup];
            for (int jx = 0; jx <= numberGroup - 1; jx++)
            {
                arr[jx] = jx;
            }
            return arr;
        }
        /// <summary>
        /// lấy số thứ tự group mà trang hiện tại thuộc về
        /// </summary>
        /// <param name="totalRecord"></param>
        /// <param name="groupPage"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public static int getGroupBelongTo(int currentPage, int groupPage)
        {
            return currentPage / groupPage;
        }
        public static int pageSizeGroup = 6;
        /// <summary>
        /// Phân trang với nhiều tham số cho Ajax function
        /// and pagesize tùy chỉnh
        /// vả có thể order
        /// </summary>
        /// <param name="sSql"></param>
        /// <param name="currentPage"></param>
        /// <param name="jsAjax"></param>
        /// <param name="arrParam"></param>
        /// <returns></returns>
        public static string pagingSqlMutilParamForAjaxAndOrderable(string sSql, int currentPage,int page_size, string jsAjax, string[] arrParam)
        {
            string sParam = "";
            if (arrParam != null)
            {
                foreach (string s in arrParam)
                {
                    //if (sParam == "") sParam = s;
                    //else 
                    if (s == "")sParam+=",\"\"";// continue;
                    else sParam += "," + s;
                }
            }

            //paging
            int pg = 1;
            //if (HttpContext.Current.Request.QueryString["page"] != null) pg = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]) - 1;
            //int page_size = comm.GetVariantPageSize();
            pg = currentPage -1;
            int start = page_size * pg;
            //DataTable dtBidding = DBUtil.LoadPage(sSql, start, page_size);

            //end paging

            //string sSqlTotal = "select count(id) from (" + sSql + ") as recTemp";
            DataTable dt = DBUtil.SelectTable(sSql);//sSqlTotal
            int totalRecord = dt.Rows.Count;

            string sPaging = "<div style='margin:0 auto;text-align:right;'>";
            sPaging += "<div id='pagination'>";
            if (totalRecord > page_size)
            {
                int totalIndex = totalRecord / page_size + (totalRecord % page_size==0?0 :1);

                //lấy số group có thể chia
                int[] numGroup = getGroupNumber(totalIndex, pageSizeGroup);

                //lấy group của trang đang load hiện tại
                int currentGroup = getGroupBelongTo(currentPage, pageSizeGroup);

                //nếu ko là đầu
                if (currentGroup != 0 && numGroup.Length > 1) { sPaging += "<a href='javascript:" + jsAjax + "(" + (0 + 1).ToString() + sParam + ");'><span class=\"prev\"></span></a> "; }

                
                //in ra số trang trong group hiện tại
                for (int jp = 0; jp <= pageSizeGroup-1; jp++)
                {
                    if (currentGroup * pageSizeGroup + (jp + 1) > totalIndex) break;
                    //if ((currentGroup * pageSizeGroup + (jp + 1)) * page_size > totalRecord) break;

                    string indexPage = (currentGroup*pageSizeGroup+ (jp + 1)).ToString();
                    if ((currentGroup * pageSizeGroup + (jp + 1)) != currentPage)
                    {

                        sPaging += "<a href='javascript:" + jsAjax + "(" + (currentGroup * pageSizeGroup + jp + 1).ToString() + sParam + ");'>" + indexPage + "</a>";
                    }
                    else
                    {
                        sPaging += "<a class=\"currentpage\">"+indexPage+"</a>";
                    }
                    if (jp != pageSizeGroup - 1) sPaging = sPaging + " ";
                }

                //nếu ko là cuối
                if (currentGroup != numGroup[numGroup.Length - 1] && numGroup.Length > 1) { sPaging += " <a href='javascript:" + jsAjax + "(" + ((currentGroup+1) * pageSizeGroup).ToString() + sParam + ");'><span class=\"next\"></span></a>"; }

                /*
                for (int jp = 0; jp < totalIndex; jp++)
                {
                    string indexPage = (jp + 1).ToString();
                    if ((jp + 1) != currentPage)
                    {

                        sPaging += "<a href='javascript:" + jsAjax + "(" + (jp + 1).ToString() + sParam + ");'>" + indexPage + "</a>";
                    }
                    else
                    {
                        sPaging += "<span class='maudophantrang'>" + indexPage + "</span>";
                    }
                    if (jp != totalIndex) sPaging= sPaging+ " . ";
                }
                */
            }
            sPaging += "</div>";
            sPaging += "</div>";
            return sPaging;
        }
        public static int getCurrentPaging()
        {
            int pg = 1;
            if (HttpContext.Current.Request.QueryString["page"] != null) pg = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]) - 1;
            //int page_size = comm.GetVariantPageSize();
            //return page_size;
            return pg;
        }
        public static int getCurrentPageSize()
        {
            int page_size = comm.GetVariantPageSize();
            return page_size;
        }
    }
